<?php

!defined('IN_NOVA') && exit('Access Denied!');

class Tags {

	public function __construct() {}

	public function __destruct() {}

	public function start() {
		global $theme,$cache,$request;
		if( $request->get( 'p' ) == NULL ) {
			$theme->title = L( '_TAGS_CLOUD_' );
			$theme->content = '<div id="tags"><h3>' . L( '_TAGS_CLOUD_' ) . '</h3>' . $this->get_tags() . '</div>';
			$theme->load('page');
		} else {
			// 参数处理
			$theme->current_page = max( 1, $request->page );
			$theme->max_per_page = $cache->config['maxperpage'];

			$tag_name = urldecode( $request->get( 'p' ) );
			$tag_num = -1;
			foreach( $cache->tag as $key => $value ) {
				if( $value['t_name'] == $tag_name ) {
					$tag_id = $key;
					$tag_num = $value['t_num'];
					break;
				}
			}
			if( $tag_num == -1 ) {
				$theme->err_404();
				return;
			} else {
				$theme->title = $tag_name . '|' . L( '_TAGS_' );
			}
			$theme->id = $tag_id;
			$theme->article_num = $tag_num;
			$theme->params = urlencode( $tag_name );
			$theme->extra = '';
			$theme->load( 'index' );
		}
	}

	// 标签云
	public function get_tags(){
		global $cache;
		$tags = '';
		foreach( $cache->tag as $tag){
			$tags .= '<a href="' .  U( APP_PATH, 'tags', urlencode( $tag['t_name'] ) ) . '" title="共包含 ' . $tag['t_num'] . ' 篇日志"><span style="font-size:' . $this->get_tag_size( $tag['t_num'] ) . 'px">' . $tag['t_name'] . ' [' . $tag['t_num'] . ']</span></a>&nbsp;&nbsp;&nbsp;';
		}
		return $tags;
	}

	private function get_tag_size( $num ){
	    for( $i = 1; $i < 11; $i ++ ){
	        if( $num < ( $i * 2.5 ) ) {
	            return 12 + $i;
			}
		}
	    return 22;
	}

}
?>